package cn.sciento.transfer.infra.datasource.provider;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import cn.sciento.transfer.infra.datasource.factory.DataSourceFactory;
import cn.sciento.transfer.infra.datasource.properties.BasicDataSourceProperties;
import cn.sciento.transfer.infra.datasource.properties.DataSourcePoolType;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

public class DefaultDataSourceProvider implements AbstractDataSourceProvider {
  private DataSourceProperties dataSourceProperties;
  
  public static final String DEFAULT_DATASOURCE_KEY = "Default";
  
  public DefaultDataSourceProvider(DataSourceProperties dataSourceProperties) {
    this.dataSourceProperties = dataSourceProperties;
  }
  
  public Map<String, DataSource> loadDataSources() {
    BasicDataSourceProperties basicDataSourceProperties = new BasicDataSourceProperties();
    basicDataSourceProperties.setDriverClassName(this.dataSourceProperties.getDriverClassName());
    basicDataSourceProperties.setUrl(this.dataSourceProperties.getUrl());
    basicDataSourceProperties.setUsername(this.dataSourceProperties.getUsername());
    basicDataSourceProperties.setPassword(this.dataSourceProperties.getPassword());
    basicDataSourceProperties.setDataSourcePoolType(DataSourcePoolType.DEFAULT);
    DataSource dataSource = DataSourceFactory.createDataSource(basicDataSourceProperties);
    Map<String, DataSource> dataSourceMap = new HashMap<>();
    dataSourceMap.put("Default", dataSource);
    return dataSourceMap;
  }
}
